import { config } from '../config.js';

// 封装请求方法 使用promise解决异步函数回调问题
export class HTTP {
    request({url, method='POST', data={}}) {
        return new Promise((resolve, reject) => {
            this._request(url, method, data, resolve, reject)
        })
    }
    _request(url, method, data, resolve, reject) {
        wx.request({
            url: config.API_URL + url,
            method: method,
            data: data,
            header: {
                'content-type': 'application/x-www-form-urlencoded'
            },  
            success: res => {
                if(res.statusCode == 200) {
                    resolve(res.data)
                }
            },
            fail: err => {
                reject(err)
                wx.showToast({
                  title: '服务器繁忙,请联系开发者!',
                  icon: 'none',
                  duration: 2000
                })
            }
        })
    }
}